/**
 * Tui
 * 
 * @created Mar. 2, 2007
 * @author adelaye
 * @author jprieur
 *
 */
package tui;

import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;

import kernel.Simulator;
import kernel.SensorSimulator;

public class Tui
{
    
    private Simulator _kernelStd;
    private SensorSimulator _kernelSen;
    static int s=0;

    /**
     * Constructor for the Tui class
     * @param filename the configuration file to use for the simulation
     */
    public Tui(File file, String outputDir,int s)
    {
         if(s==0)
         {
             _kernelStd = new Simulator(outputDir);
             SAXParserFactory factory = SAXParserFactory.newInstance();
	     //	     factory.setValidating(true); // DTD check

             try{
	         SAXParser parser = factory.newSAXParser();
	         parser.parse(file, new TuiContentHandler(_kernelStd.getSimulator(),outputDir));
	         _kernelStd.getSimulator().runSim();
                 _kernelStd.getSimulator().getResultManager().setResultDir(outputDir);
	         _kernelStd.getSimulator().generateResults();
	     }
	     catch (SAXParseException spe) {
                 // Error generated by the parser
	         System.out.println("\n** Parsing error"
			       + ", line " + spe.getLineNumber()
			       + ", uri " + spe.getSystemId());
   	         System.out.println("   " + spe.getMessage() );
	         // Use the contained exception, if any
	         Exception  x = spe;
	         if (spe.getException() != null)
		   x = spe.getException();
	    
  	         // stacktrace may be too much information as a XML parsing
	         // error...
	         //x.printStackTrace();

             }
             catch (SAXException sxe) {
	         // Error generated by this application
	         // (or a parser-initialization error)
	         Exception  x = sxe;
	         if (sxe.getException() != null)
		     x = sxe.getException();
	         x.printStackTrace();
             }
             catch (ParserConfigurationException pce) {
                 // Parser with specified options can't be built
                 pce.printStackTrace();
             }
             catch (IOException ioe) {
	         // I/O error
	         ioe.printStackTrace();
             }
         }
         if(s==1)
         {
             _kernelSen = new SensorSimulator(outputDir);
             SAXParserFactory factory = SAXParserFactory.newInstance();
	     factory.setValidating(true); // DTD check

             try{
	         SAXParser parser = factory.newSAXParser();
	         parser.parse(file, new TuiContentHandler(_kernelSen.getSimulator(),outputDir));
	         ((SensorSimulator)(_kernelSen.getSimulator())).runSim();
	         ((SensorSimulator)(_kernelSen.getSimulator())).getResultManager().setResultDir(outputDir);
	         ((SensorSimulator)(_kernelSen.getSimulator())).generateResults();
	     }
	     catch (SAXParseException spe) {
                 // Error generated by the parser
	         System.out.println("\n** Parsing error"
			       + ", line " + spe.getLineNumber()
			       + ", uri " + spe.getSystemId());
   	         System.out.println("   " + spe.getMessage() );
	         // Use the contained exception, if any
	         Exception  x = spe;
	         if (spe.getException() != null)
		   x = spe.getException();
	    
  	         // stacktrace may be too much information as a XML parsing
	         // error...
	         //x.printStackTrace();

             }
             catch (SAXException sxe) {
	         // Error generated by this application
	         // (or a parser-initialization error)
	         Exception  x = sxe;
	         if (sxe.getException() != null)
		     x = sxe.getException();
	         x.printStackTrace();
             }
             catch (ParserConfigurationException pce) {
                 // Parser with specified options can't be built
                 pce.printStackTrace();
             }
             catch (IOException ioe) {
	         // I/O error
	         ioe.printStackTrace();
             }
         }
    }

    public static void main(String args[])
    {
	if(args.length == 0){
	    System.out.println("A SimMobil TUI session needs an input file.");
	    System.exit(-1);
	}

	File input = new File(args[0]);

	String output;
	if(args.length > 0){
	    output = args[1];
	}else{
	    output = "";
	}
	
	if(args.length==2 && (args[0].substring(args[0].length()-1,args[0].length())).equals("3"))
	{
	    s=1;
	}

	if(!input.exists()){
	    System.out.println("The file doesn't exist : " + args[0]);
	    System.exit(-1);
	}else{
	    new Tui(input,output,s);
	}
    }

}
